import { wrap, uploadImage } from "./requestWrapper";
import { getFormatDate } from "@/utils";
import { TMotion, TRecord, TPlan, TCreateRecord } from "@/types";

export { uploadImage };
export const wxlogin = async (
  data
): Promise<{ token: string; error: string }> =>
  wrap("/auth/wxlogin", {
    data,
    method: "POST",
  });

// Motion
export const getMotions = async (): Promise<Array<TMotion>> =>
  wrap("/api/getMotions", {
    method: "GET",
  });

export const getMotion = async (id: string): Promise<TMotion> =>
  wrap(`/api/getMotions/${id}`, {
    method: "GET",
  });

export const addMotion = async (data: TMotion) =>
  wrap(`/api/addMotion`, {
    method: "POST",
    data,
  });

export const updateMotion = async (data: TMotion) =>
  wrap(`/api/updateMotion`, {
    method: "POST",
    data,
  });

export const delMotion = async (id: string) =>
  wrap(`/api/deleteMotion`, {
    method: "POST",
    data: {
      motionId: id,
    },
  });

// Record
export const getTodayRecords = async (): Promise<TRecord[]> => {
  const today = getFormatDate();
  return wrap(`/api/getRecords?date=${today}`, {
    method: "GET",
  });
};

export const getRecordsByDay = async (date?: string): Promise<TRecord[]> => {
  if (!date) date = getFormatDate();
  return wrap(`/api/getRecords?date=${date}`, {
    method: "GET",
  });
};

export const getRecord = async (id: string): Promise<TCreateRecord> => {
  return wrap(`/api/getRecords/${id}`, {
    method: "GET",
  });
};

export const addRecords = async (data: {
  records: TCreateRecord[];
  date?: string;
}) => {
  return wrap(`/api/addRecords`, {
    method: "POST",
    data,
  });
};

export const updateRecord = async (data: TCreateRecord) =>
  wrap(`/api/updateRecord`, {
    method: "POST",
    data,
  });

export const delRecord = async (id: string) =>
  wrap(`/api/deleteRecord`, {
    method: "POST",
    data: {
      recordId: id,
    },
  });

export const getMonthStats = async (
  month: string
): Promise<{ [key: string]: number }> =>
  wrap(`/api/getMonthStats?month=${month}`, {
    method: "GET",
  });

export const getPlans = async (): Promise<Array<TPlan>> =>
  wrap("/api/getPlans", {
    method: "GET",
  });

export const getPlan = async (id: string): Promise<TPlan> =>
  wrap(`/api/getPlans/${id}`, {
    method: "GET",
  });

export const addPlan = async (data) =>
  wrap(`/api/addPlan`, {
    method: "POST",
    data,
  });

export const updatePlan = async (data) =>
  wrap(`/api/updatePlan`, {
    method: "POST",
    data,
  });

export const delPlan = async (id: string) =>
  wrap(`/api/deletePlan`, {
    method: "POST",
    data: {
      planId: id,
    },
  });
